﻿using Autofac;
using Ekton_Manage_Web.Extensions.ApplicationBuilderExtensions;
using Ekton_Manage_Web.Extensions.ServiceCollectionExtensions;
using Ekton_Manage_Web.Filter;
using Hangfire;
using Hangfire.MemoryStorage;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Ekton_Manage_Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCorsSetup();
            services.AddControllers(options =>
            {
                options.Filters.Add<GlobalExceptionFilter>();
                #region ����У��
                options.Filters.Add<ValidateModelFilter>();
            }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
       .ConfigureApiBehaviorOptions(e =>
       {
           //�ر�Ĭ��ģ����֤
           e.SuppressModelStateInvalidFilter = true;
       });
            #endregion
            services.AddMemoryCacheSetup();
            services.AddUseContext();
            services.AddSwagger(Configuration);
            //services.AddTimedJob();

            services.AddInjectionSetup();
            services.AddMiniProfilerSetup();
            services.AddHangfire(x => x.UseStorage(new MemoryStorage()));
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
            #region Business��
            var basePath = ApplicationEnvironment.ApplicationBasePath;
            var servicesDllFile = Path.Combine(basePath, "Ekton_Manage_Business.dll");
            var assemblysServices = Assembly.LoadFrom(servicesDllFile);
            builder.RegisterAssemblyTypes(assemblysServices)
            .AsImplementedInterfaces()
            .InstancePerDependency();
            #endregion

            #region Mapper
            var assemblysMapper = Assembly.Load("Ekton_Manage_Mapper");
            builder.RegisterAssemblyTypes(assemblysMapper);
            #endregion

            #region Framework
            var assemblysFramework = Assembly.Load("Ekton_Manage_Framework");
            builder.RegisterAssemblyTypes(assemblysFramework);
            #endregion

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                //app.UseSwagger();
                //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ekton_Manage_Web v1"));
            }
            // ��װSwaggerչʾ
            app.UseSwaggerMildd(() => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Ekton_Manage_Web.index.html"));

            app.UseHttpsRedirection();
            //app.UseTimedJob();
            app.UseRouting();
            app.UseCors("cors");
            app.UseErrorHandling();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseHangfireServer();
            app.UseHangfireDashboard();
            app.TimeJobMiddleware();
            // ���ܷ���
            app.UseMiniProfiler();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
